<?php
/**
 * Created by PhpStorm.
 * User: Windows
 * Date: 2017/4/25
 * Time: 16:19
 */

namespace Task;

use \Workerman\Worker;
use \Workerman\Connection\UdpConnection;
use \Workerman\Connection\AsyncTcpConnection;
class TaskHttp extends Worker{

    public function __construct($socket_name)
    {
        parent::__construct($socket_name);
        $this->onWorkerStart = array($this, 'onStart');
        $this->onConnect = array($this, 'onConnect');
        $this->onMessage = array($this, 'onMessage');
        $this->onClose = array($this, 'onClose');
        $this->onWorkerStop = array($this, 'onStop');
        $this->onError = array($this, 'onError');
    }

    public function onStart(){
        echo "Worker starting..."."\n";
    }

    public function onConnect($connection){
        echo "new connection from ip " . $connection->getRemoteIp() . "\n";
    }

    public function onMessage($connection,$data){
        $socket = stream_socket_client('udp://127.0.0.1:8010');
        // 任务及参数数据
        if(empty($_REQUEST)){
            $task_data = array(
                'get'=>'getdata'
            );
        }else{
            $task_data = $_REQUEST;
        }
        if(!$socket)
        {
            return false;
        }

        stream_socket_sendto($socket, json_encode($task_data));

       $connection->send('task complete');
    }

    public function onClose($connection){
        echo "close success"."\n";
    }

    public function onStop(){
        echo 'Worker stopping...'."\n";
    }

    public function onError($connection, $code, $msg){
        echo "error $code $msg\n";
    }
}